package com.googlecode.monitoring.flash.dao;

import com.googlecode.monitoring.dao.XmlUtils;
import com.googlecode.monitoring.exception.DataDaoException;
import com.googlecode.monitoring.flash.model.FlashItemToCheck;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.Date;
import java.util.List;


@Service
public class FlashAgentXmlUtils {

    private XmlUtils xmlUtils;

    @Autowired
    public void setXmlUtils(XmlUtils xmlUtils) {
        this.xmlUtils = xmlUtils;
    }

    public FlashItemToCheck buildDataToCheck(Element xmlItemElmnt) {

        String name = xmlItemElmnt.getAttribute("name");
        if (name.isEmpty())
            throw new DataDaoException("item with empty name");
        String id = xmlItemElmnt.getAttribute("id");
        if (id.isEmpty())
            throw new DataDaoException("item with empty id");

        String url = xmlItemElmnt.getElementsByTagName("page").item(0).getTextContent();
        Node timeEventNode = xmlItemElmnt.getElementsByTagName("eventTime").item(0);
        NodeList personNode = xmlItemElmnt.getElementsByTagName("person");
        String description = xmlItemElmnt.getElementsByTagName("description").item(0).getTextContent();

        FlashItemToCheck flashItemToCheck = new FlashItemToCheck();
        flashItemToCheck.setName(name);
        flashItemToCheck.setId(id);
        flashItemToCheck.setEventTime(xmlUtils.buildEventTime(timeEventNode));
        flashItemToCheck.setResponsiblePersons(xmlUtils.buildPersons(personNode));
        flashItemToCheck.setDescr(description);
        flashItemToCheck.setUrl(url);
        
        return flashItemToCheck;
    }

    public Element buildFlashItemToCheckElement(Document doc, FlashItemToCheck flashItemToCheck, SchedulerFactoryBean schedulerFactoryBean) throws Exception {
        Element itemEl = doc.createElement("item");
        Element pageEl = doc.createElement("page");
        Element timeEventEl = doc.createElement("eventTime");
        Element stateEl = doc.createElement("state");
        Element descrEl = doc.createElement("description");
        Element lastCheckTimeEl = doc.createElement("lastCheckTime");
        Element nextCheckTimeEl = doc.createElement("nextCheckTime");

        lastCheckTimeEl.setAttribute("value", new DateTime(new Date()).toString(XmlUtils.dateTimeFormat));
        nextCheckTimeEl.setAttribute("value", xmlUtils.getNextCheckTime(flashItemToCheck, schedulerFactoryBean).toString(XmlUtils.dateTimeFormat));

        itemEl.setAttribute("id", flashItemToCheck.getId());
        itemEl.setAttribute("name", flashItemToCheck.getName());

        timeEventEl.setAttribute("cron", flashItemToCheck.getEventTime().getCron());

        List<Element> personElList = xmlUtils.buildPersonNodeList(doc, flashItemToCheck);
        if (flashItemToCheck.getCurrentStateName() != null) {
            stateEl.setAttribute("name", flashItemToCheck.getCurrentStateName().getName());
        }
        descrEl.setTextContent(flashItemToCheck.getDescr());

        itemEl.appendChild(timeEventEl);
        for (Element personEl : personElList) {
            itemEl.appendChild(personEl);
        }

        pageEl.setTextContent(flashItemToCheck.getUrl());
        itemEl.appendChild(stateEl);
        itemEl.appendChild(pageEl);
        itemEl.appendChild(descrEl);
        itemEl.appendChild(lastCheckTimeEl);
        itemEl.appendChild(nextCheckTimeEl);

        return itemEl;
    }
    
}
